package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Shop;
import cn.wolfcode.luowowo.article.mapper.ShopMapper;
import cn.wolfcode.luowowo.article.query.ShopQuery;
import cn.wolfcode.luowowo.article.service.IShopService;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class ShopServiceImpl implements IShopService{

    @Autowired
    private ShopMapper shopMapper;

    @Reference
    private IUserInfoService userInfoService;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return 0;
    }

    @Override
    public int insert(Shop record) {
        return 0;
    }

    @Override
    public Shop selectByPrimaryKey(Long id) {
        return shopMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Shop> selectAll() {
        return shopMapper.selectAll();
    }

    @Override
    public int updateByPrimaryKey(Shop record) {
        return 0;
    }

    //条件查询
    @Override
    public PageInfo queryByType(Long userId, ShopQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());

        //根据用户的shopId查询商品
        UserInfo userInfo = userInfoService.getScoreById(userId);
        String shopId = userInfo.getShopId();
        String[] split = shopId.split(",");

        Long[] shop = new Long[split.length];

        for(int i = 0; i< split.length; i++){
            shop[i] = Long.valueOf(split[i]);
        }

        List<Shop> list = shopMapper.selectForList(shop);

        return new PageInfo(list);
    }

    @Override
    public PageInfo queryAll(ShopQuery qo) {

        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());

        List<Shop> list = shopMapper.selectAll();

        return new PageInfo(list);
    }

    @Override
    public void updateRest(Long shopId) {
        shopMapper.updateRest(shopId);
    }
}
